package vn.mhst24.json;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

import org.json.JSONArray;

import android.app.ProgressDialog;

public class GetLatLng2 {
	// URL to get contacts JSON
	private static String xml_url = "https://maps.googleapis.com/maps/api/geocode/xml?address=";
	private static String api_key = "AIzaSyB36gqXnG07xGhiE6LaI4GEikfLYVatQfk";
	// JSON Node names
	private static final String TAG_RESULTS = "results";
	private static final String TAG_GEO = "geometry";
	private static final String TAG_LAT = "lat";
	private static final String TAG_LNG = "lng";
	JSONArray contacts = null;

	String urls, urld;

	private ProgressDialog pDialog;
	private HandleXML objs, objd;
	String adSpoin = null, adDpoin = null;

	public GetLatLng2() {

	}

	public String GetLat(String sPoint) {
		try {
			adSpoin = addressEncode(sPoint);
			System.out.println("adSpoin" + adSpoin);
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		urls = xml_url + adSpoin + "&key=" + api_key;
		System.out.println("urls" + urls);
		objs = new HandleXML(urls);
		objs.fetchXML();
		while (objs.parsingComplete)
			;
		return objs.getLat();

	}

	public String GetLng(String dPoint) {
		try {
			adDpoin = addressEncode(dPoint);
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		urld = xml_url + adDpoin + "&key=" + api_key;

		objd = new HandleXML(urld);
		objd.fetchXML();

		while (objd.parsingComplete)
			;
		return objd.getLng();

	}

	@SuppressWarnings("unused")
	private String addressEncode(String address)
			throws UnsupportedEncodingException {

		return address = URLEncoder.encode(address, "utf-8");

	}

}
